package com.xisan.practice.rabbitmq.helloworld;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import com.xisan.practice.rabbitmq.utils.RabbitMQUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 直连模型
 */
public class Provider {

    @Test
    public void sendMessage() throws IOException {

        //1.工具类获取连接对象
        Connection connection = RabbitMQUtils.getConnection();
        //2.创建通道
        assert connection != null;
        Channel channel = connection.createChannel();
        //3.通道绑定队列
        //队列、是否持久化、是否独占、是否自动删除(当消费者不占用队列后才会删除)、附加信息
        channel.queueDeclare("hello", true, false, true, null);
        //4.发布消息
        //交换机、队列、传递消息额外设置(消息持久化)、消息的内容
        channel.basicPublish("", "hello", MessageProperties.PERSISTENT_TEXT_PLAIN, "hello rabbitmq".getBytes());
        //5.关闭
        RabbitMQUtils.close(channel, connection);
    }
}
